package top.infopub.mgr.common.tags;


import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang3.StringUtils;

import top.infopub.model.sys.ParameterGroupDetail;
import top.infopub.tdp.util.GrpParaUtil;


/**
 * 根据数据字典组编号名称展示全部的值
 * 返回{"add":"新增", "update":"修改"} 此种json
 * @author Awoke
 * @version 2018年10月15日
 * @see DicJson
 * @since
 */
public class DicJson extends TagSupport {

    private static final long serialVersionUID = -8802895125792934841L;

    /* 数据字典编号 */
    private String groupNo;

    @Override
    public int doStartTag()
        throws JspException {
        try {
            StringBuilder valStr = new StringBuilder("{");
            Set<ParameterGroupDetail> parameterGroupDetailList = GrpParaUtil.get(groupNo);
            Iterator<ParameterGroupDetail> iterator = parameterGroupDetailList.iterator();
            while (iterator.hasNext()) {
                ParameterGroupDetail parameterGroupDetail = iterator.next();
                valStr.append("\"" + String.valueOf(parameterGroupDetail.getSvalue()) + "\":\""
                              + parameterGroupDetail.getSname() + "\"");
                valStr.append(",");
            }
            // {"add":"新增", "update":"修改"}
            String json = StringUtils.removeEnd(valStr.toString(), ",");
            json = json + "}";
            pageContext.getOut().write(json);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return super.doStartTag();
    }

    @Override
    public void release() {
        super.release();
        groupNo = "";
    }

    @Override
    public int doEndTag()
        throws JspException {
        return super.doEndTag();
    }

    public String getGroupNo() {
        return groupNo;
    }

    public void setGroupNo(String groupNo) {
        this.groupNo = groupNo;
    }

}
